<?php

namespace App\Api\Sms;
class Base
{
    public function __construct()
    {

    }

    protected function returnMsg($code = 200, $msg = '', $data = [])
    {
        return [
            'code' => $code,
            'msg' => $msg,
            'data' => $data
        ];
    }

//获取毫秒的时间戳
    public static function getMillisecond()
    {
        //获取毫秒的时间戳
        $time = explode(" ", microtime());
        $time = $time[1] . ($time[0] * 1000);
        $time2 = explode(".", $time);
        return $time2[0];
    }

//解析模板参数,把内容用参数的值替换,主要是自己保存发送内容记录时用
    public function parseTplContent($content, $tplParamsData)
    {
        $paramsData = $this->parseTplParamsValue($content, $tplParamsData);
        if (empty($paramsData)) {
            return $content;
        }
        foreach ($paramsData as $key => $value) {
            $content = str_replace('{%' . $key . '%}', $value, $content);
        }
        return $content;
    }

//解析模板参数,用来提交接口时的参数内容
    public function parseTplParams($content, $tplParamsData)
    {
        $paramsData = $this->parseTplParamsValue($content, $tplParamsData);
        if (!empty($paramsData)) {
            return array_values($paramsData);
        } else {
            return [];
        }
    }

//解析模板的参数对应的值
    public function parseTplParamsValue($content, $tplParamsData)
    {
        //        分析变量,变量固定字段已在card.smsTplParams定义
        $params = [];
        if (!empty($tplParamsData)) {
            preg_match_all("/\{%(.+?)%}/", $content, $matches);
            if (!empty($matches[1]) && is_array($matches[1])) {
                $smsTplParams = config('card.smsTplParams');
                $expressStatusList = [];
                $activatStatusList = [];
                $firstStatusList = [];
                foreach ($matches[1] as $key => $name) {
                    $paramValue = '';
                    if (isset($smsTplParams[$name])) {
                        $dataField = $smsTplParams[$name];
                        // 取值
                        switch ($dataField) {
                            case 'users_phone':
                                if (!empty($tplParamsData['users_phone']) && empty($tplParamsData[$dataField])) {
                                    $tplParamsData[$dataField] = $tplParamsData['users_phone'];
                                } else {
                                    $tplParamsData[$dataField] = config('card.phone400');
                                }
                                break;
                            case 'iccid':
                                if (!empty($tplParamsData['sku_number']) && (empty($tplParamsData[$dataField]) || strlen($tplParamsData[$dataField]) < 10)) {
                                    $tplParamsData[$dataField] = $tplParamsData['sku_number'];
                                }
                                break;
                            case 'express_status':
                                if (isset($tplParamsData['express_status_desc'])) {
                                    $tplParamsData[$dataField] = $tplParamsData['express_status_desc'];
                                } else {
                                    if (empty($expressStatusList)) {
                                        $expressStatusList = config('express.status');
                                    }
                                    if (isset($expressStatusList[$tplParamsData[$dataField]])) {
                                        $tplParamsData[$dataField] = $expressStatusList[$tplParamsData[$dataField]];
                                    }
                                }
                                break;
                            case 'activat_status':
                                if (isset($tplParamsData['activat_status_desc'])) {
                                    $tplParamsData[$dataField] = $tplParamsData['activat_status_desc'];
                                } else {
                                    if (empty($activatStatusList)) {
                                        $activatStatusList = config('card.activatStatus');
                                    }
                                    if (isset($activatStatusList[$tplParamsData[$dataField]])) {
                                        $tplParamsData[$dataField] = $activatStatusList[$tplParamsData[$dataField]];
                                    }
                                }
                                break;
                            case 'first_status':
                                if (isset($tplParamsData['first_status_desc'])) {
                                    $tplParamsData[$dataField] = $tplParamsData['first_status_desc'];
                                } else {
                                    if (empty($firstStatusList)) {
                                        $firstStatusList = config('card.firstStatus');
                                    }
                                    if (isset($firstStatusList[$tplParamsData[$dataField]])) {
                                        $tplParamsData[$dataField] = $firstStatusList[$tplParamsData[$dataField]];
                                    }
                                }
                                break;
                            default:
                                break;
                        }
                        $paramValue = $tplParamsData[$dataField] ?? '';
                    }
                    $params[$name] = $paramValue;
                }
            }
        }
        return $params;
    }

}
